package rhinestone.Model;

import java.util.HashMap;
import java.util.Iterator;

/**
 * This class holds the products, which makes it a pack.
 *
 * @author Group 2
 * @version 2010.12.16
 */
public class Pack extends Item {
	private HashMap<Product, Integer> products;
	private static final double PERCENT_OF_TOTAL_PRICE = 90;

	/**
	 * Constructor.
	 *
	 * @param barcode Bar code of the product in a pack.
	 * @param name The name of the pack.
	 * @param description The description of the pack.
	 */
	public Pack(String barcode, String name, String description) {
		super(barcode, name, description, 0);
		products = new HashMap<Product, Integer>();
	}

	/**
	 * Adds a product to the pack.
	 *
	 * @param barcode The bar code of the product.
	 * @param amount Amount of products with the provided bar code in the pack.
	 */
	public void addProduct(Product product, int amountToAdd) {
		if(products.containsKey(product)) {
			int amountInPack = products.get(product);
			amountToAdd += amountInPack;
		}
		products.put(product, amountToAdd);
	}

	/**
	 * Removes product from the pack.
	 * If amountToRemove = 0, removes that product from the pack list.
	 * Otherwise, removes just the amount provided.
	 * 
	 * @param barcode The bar code of the product to remove.
	 * @param amountToRemove The amount of that product to remove.
	 */
	public void removeProduct(Product product, int amountToRemove) {
		int amountInPack = products.get(product);
		if(amountInPack > 0) {
			int newAmount = amountInPack - amountToRemove;

			if(amountToRemove < 0)
				System.err.println("Cannot remove negative amount of products!");
			else if(amountToRemove == 0 || newAmount <= 0)
				products.remove(product);
			else
				products.put(product, newAmount);
		}
		else
			System.err.println("No product with bar code " + product.getBarcode() + " in the pack.");
	}

	@Override
	public double getPrice() {
		double price = 0;
		Iterator<Product> it = products.keySet().iterator();
		while(it.hasNext()) {
			Product product = it.next();
			price += product.getPrice() * products.get(product);
		}
		price = price / 100 * PERCENT_OF_TOTAL_PRICE;
		return price;
	}

	public int getAvailablAmount() {
		Iterator<Product> it = products.keySet().iterator();
		int maxAvailable = Integer.MAX_VALUE;
		while(it.hasNext()) {
			Product product = it.next();
			int available = product.getTotalAmount() / products.get(product);
			if(available < maxAvailable)
				maxAvailable = available;
		}
		return maxAvailable;
	}

	public HashMap<Product, Integer> getProducts() {
		return products;
	}

	public int getAmountInPack(Product product) {
		return products.get(product);
	}

	public int getItemCount() {
		int totalItems = 0;

		Iterator<Product> it = products.keySet().iterator();
		while(it.hasNext())
			totalItems += products.get(it.next());

		return totalItems;
	}

	@Override
	public String toString() {
		String output = "---------------------------------";
		output += "\nBar code: " + barcode;
		output += "\nName: " + name;
		output += "\nDescription: " + description;
		output += "\n---------------------------------";
		Iterator<Product> it = products.keySet().iterator();
		while(it.hasNext()) {
			Product product = it.next();
			output += "\n" + products.get(product) + "x " + product.getName() + " (" + product.getBarcode() + ")";
		}
		output += "\n---------------------------------";
		return output;
	}
}
